//  PersonageController.m
//  Created by 钓鱼人 on 15/12/18.
//  Copyright © 2015年 game. All rights reserved.

#import "PersonageController.h"
#import "UIImageView+WebCache.h"
#import "PersonHeadView.h"
#import "LoginController.h"
#import "MyConmentCtrl.h"
#import "MyInformController.h"
#import "UserDataModel.h"
#import "UIImageView+WebCache.h"
#import "PersonEditCtrl.h"
#import "PersonSetViewController.h"
#import "MyAttractOfislanViewController.h"
#import "My_threadViewController.h"
#import "ForwardToFriedsController.h"
#import "MyAddislanViewController.h"
#import "MyPmViewController.h"
#import "ForMyCommentCtl.h"
#import "MyaddController.h"
#import "SystemNewsCtrl.h"
#import "SayHilloViewController.h"
#define IMghight 200
@interface PersonageController ()<UITableViewDataSource,UITableViewDelegate,PersonHeadViewDeleget>
{
    NSArray *_imagAry;
    NSArray *_titleAry;
    NSArray *_secImgAry;
    NSArray *_secTitleAry;
    PersonHeadView *_headView;
    UserDataModel *_userModel;
}
@property (nonatomic,strong) UIImageView *bacImg;
@property (nonatomic,strong) UITableView *tableview;
@end

@implementation PersonageController

/**
 *  下拉放大的图片
 */
-(UIImageView *)bacImg{
    if (!_bacImg) {
        _bacImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, IMghight)];
        
        [_bacImg sd_setImageWithURL:[NSURL URLWithString:@"http://imgsrc.baidu.com/forum/pic/item/32fa828ba61ea8d3b28422aa970a304e241f58fd.jpg"]];
        _bacImg.contentMode = UIViewContentModeScaleAspectFill;
        [self.view addSubview:_bacImg];
    }
    return _bacImg;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.app.myTabBar.tabBar.hidden = NO;
    StatusWhite
}
#pragma mark--获取个人资料
-(void)GetUserData{
    if (_headView) {
        _headView.model = self.app.userModel;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self AddTableview];

    [self HeadView];
    [self GetUserData];
}

-(void)HeadView{
    
    _headView = [[PersonHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.6)];
    _headView.backgroundColor =[UIColor clearColor];
    _tableview.tableHeaderView = _headView;
    _headView.deleget = self;
    _headView.farherCtrl = self;
}

-(void)InformClickTop{

    if ([self.app.isLogin isEqualToString:@"YES"]) {
        
        MyInformController *inform = [MyInformController new];
        inform.model = self.app.userModel;
        __weak typeof(self) weakSelf = self;
        inform.imageChange = ^{
            
            [[SDImageCache sharedImageCache]removeImageForKey:self.app.userModel.avatar];
            [weakSelf GetUserData];
        };
        
        [self.navigationController pushViewController:inform animated:YES];
    }else{
        LoginController *login = [LoginController new];
        [self.navigationController pushViewController:login animated:YES];
    }
}

#pragma mark--我的消息点击事件
-(void)NewsClickWhithTag:(NSInteger)tag{

    if ([self.app.isLogin isEqualToString:@"YES"]) {
        
        [self GetUserData];
        if (tag==0) {//私信
            MyPmViewController *myPm = [MyPmViewController new];
            [self.navigationController pushViewController:myPm animated:YES];
            
        }else if (tag==1) {//评论
            ForMyCommentCtl *comment = [ForMyCommentCtl new];
            [self.navigationController pushViewController:comment animated:YES];
            
        }else if (tag==2){//加好友
            MyaddController *add = [MyaddController new];
            [self.navigationController pushViewController:add animated:YES];
            
        }else if (tag==3){//系统消息
            SystemNewsCtrl *sys = [SystemNewsCtrl new];
            [self.navigationController pushViewController:sys animated:YES];
        }
        
    }else{
        
        LoginController *login = [LoginController new];
        [self.navigationController pushViewController:login animated:YES];
    }

}

-(void)AddTableview{

    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    _tableview.dataSource= self;
    _tableview.delegate= self;
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableview];
    _tableview.showsVerticalScrollIndicator = NO;
    _imagAry = @[@"add_fishing",@"attention_fishing",@"add_shop",@"attention_shop"];
    _titleAry = @[@"我添加的钓场",@"我关注的钓场",@"我添加的渔具店",@"我关注的渔具店"];
    
    _secImgAry = @[@"recommend_friends",@"feedback",@"edit",@"setup"];
    _secTitleAry = @[@"推荐给好友",@"意见反馈",@"草稿箱",@"设置"];
    [self.tableview insertSubview:self.bacImg atIndex:0];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section{
    return 10;

}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 4;
    }else{
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.font = Neuefont(SCREEN_WIDTH*0.045);//font(SCREEN_WIDTH*0.05);
    
    if (indexPath.section==0) {
        cell.imageView.image = [UIImage imageNamed:_imagAry[indexPath.row]];
        cell.textLabel.text = _titleAry[indexPath.row];
    }else{
    
        cell.imageView.image = [UIImage imageNamed:_secImgAry[indexPath.section-1]];
        cell.textLabel.text = _secTitleAry[indexPath.section-1];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section==1||indexPath.section==4) {
        if (indexPath.section==1) {//推荐给好友
            ForwardToFriedsController *forward = [ForwardToFriedsController new];
            [self.navigationController pushViewController:forward animated:YES];
        }
        
        
        if (indexPath.section==4) {//设置
            
            PersonSetViewController *set = [PersonSetViewController new];
            [self.navigationController pushViewController:set animated:YES];
        }

    }else{
        
        if (![self.app.isLogin isEqualToString:@"YES"]) {
            LoginController *login = [LoginController new];
            [self.navigationController pushViewController:login animated:YES];
        }else{
            
            if (indexPath.section==0) {
                
                if (indexPath.row==0) {//我添加的钓场
                    MyAddislanViewController *colle = [MyAddislanViewController new];
                    colle.Url = MyAddIslan;
                    [self.navigationController pushViewController:colle animated:YES];
                }

                if (indexPath.row==1) {//我关注的钓场
                    MyAttractOfislanViewController *colle = [MyAttractOfislanViewController new];
                    colle.Url = MyAttractIslan;
                    [self.navigationController pushViewController:colle animated:YES];
                }
                
                if (indexPath.row==2) {//我添加的渔具店
                    MyAddislanViewController *colle = [MyAddislanViewController new];
                    colle.Url = MyAddDianpu;
                    colle.IsYujuDian = @"YES";
                    [self.navigationController pushViewController:colle animated:YES];
                }
                
                if (indexPath.row==3) {//我关注的渔具店
                    MyAttractOfislanViewController *colle = [MyAttractOfislanViewController new];
                    colle.Url = MyAttractDianpu;
                    colle.IsYujuDian = @"YES";
                    [self.navigationController pushViewController:colle animated:YES];
                }
        }
            if (indexPath.section==3) {//草稿箱
            
            PersonEditCtrl *edit = [PersonEditCtrl new];
            [self.navigationController pushViewController:edit animated:YES];
        }
        
        if (indexPath.section==2) {//意见反馈
            
            SayHilloViewController *hillo = [SayHilloViewController new];
            hillo.type = @"sysm";
            hillo.otherId = @"11";
            [self.navigationController pushViewController:hillo animated:YES];
        }
    }
    }
}

#pragma mark--下拉图片变大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    
    CGFloat offsetH = -IMghight *0 - offsetY;
    CGRect frame = self.bacImg.frame;
    frame.size.height = IMghight + offsetH;
    frame.origin.y = -offsetH;
    self.bacImg.frame = frame;
}

@end
